package com.derbtec.refale.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.json.JSONException;
import org.json.JSONObject;


import com.derbtec.refale.models.Event;
import com.derbtec.refale.models.Participant;
import com.derbtec.refale.models.User;

import android.app.Application;
import android.util.Log;

public class HelperUtils extends Application {

	public User UserFromJson(JSONObject json_data, User userbuffer) throws NumberFormatException, JSONException, ParseException {
		userbuffer.setId(Integer.parseInt(json_data.getString("UId")));
		userbuffer.setUsername(json_data.getString("Username"));

		int adminbuffer = 0;
		adminbuffer = json_data.getInt("isAdmin");
		if(adminbuffer == 1)
			userbuffer.setAdmin(true);
		else
			userbuffer.setAdmin(false);
		
		userbuffer.seteMail(json_data.getString("EMail"));
		userbuffer.setFirstName(json_data.getString("Firstname"));
		userbuffer.setLastName(json_data.getString("Lastname"));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		userbuffer.setBirthDay(sdf.parse(json_data.getString("Birthday")));
		userbuffer.setLastOnline(sdf.parse(json_data.getString("LastOnline")));
		
		return userbuffer;
	}
	
	public static Participant ParticipantFromJson(JSONObject json_data, Participant participantbuffer) throws NumberFormatException, JSONException, ParseException {
		participantbuffer.setId(Integer.parseInt(json_data.getString("UId")));
		participantbuffer.setUsername(json_data.getString("Username"));

		int adminbuffer = 0;
		adminbuffer = json_data.getInt("isAdmin");
		if(adminbuffer == 1)
			participantbuffer.setAdmin(true);
		else
			participantbuffer.setAdmin(false);
		
		participantbuffer.seteMail(json_data.getString("EMail"));
		participantbuffer.setFirstName(json_data.getString("Firstname"));
		participantbuffer.setLastName(json_data.getString("Lastname"));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		participantbuffer.setBirthDay(sdf.parse(json_data.getString("Birthday")));
		participantbuffer.setLastOnline(sdf.parse(json_data.getString("LastOnline")));
		participantbuffer.setEventid(Integer.parseInt(json_data.getString("Eventid")));
		participantbuffer.setFollowers(Integer.parseInt(json_data.getString("Followers")));
		participantbuffer.setAcceptdate(sdf.parse(json_data.getString("Accepttime")));
		return participantbuffer;
	}
	
	public static Event EventFromJson(JSONObject json_data, Event eventbuffer, User creator) throws NumberFormatException, JSONException, ParseException {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		  
        eventbuffer.setId(Integer.parseInt(json_data.getString("EId")));
        eventbuffer.setName(json_data.getString("Name"));
        eventbuffer.setPlace(json_data.getString("Place"));
        eventbuffer.setDescription(json_data.getString("Description"));
		eventbuffer.setStartDate(sdf.parse((json_data.getString("Starttime"))));
		eventbuffer.setEndDate(sdf.parse((json_data.getString("Endtime"))));
		eventbuffer.setCreationDate(sdf.parse(json_data.getString("Editdate")));
		
		creator.setId(Integer.parseInt(json_data.getString("UId")));
		creator.setUsername(json_data.getString("Username"));
        
		int adminbuffer = 0;
		Boolean admin = false;
		adminbuffer = json_data.getInt("isAdmin");
		if(adminbuffer == 1)
			admin = true;
		
		creator.setAdmin(admin);
        
		eventbuffer.setCreator(creator);
		
		return eventbuffer;
	}
	
	public static User UserFromJsonString(String jsondata) throws NumberFormatException, JSONException, ParseException {
		JSONObject json_data = new JSONObject(jsondata);
		User userbuffer = new User();
		userbuffer.setId(Integer.parseInt(json_data.getString("UId")));
		userbuffer.setUsername(json_data.getString("Username"));

		int adminbuffer = 0;
		adminbuffer = json_data.getInt("isAdmin");
		if(adminbuffer == 1)
			userbuffer.setAdmin(true);
		else
			userbuffer.setAdmin(false);
		
		userbuffer.seteMail(json_data.getString("EMail"));
		userbuffer.setFirstName(json_data.getString("Firstname"));
		userbuffer.setLastName(json_data.getString("Lastname"));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		userbuffer.setBirthDay(sdf.parse(json_data.getString("Birthday")));
		userbuffer.setLastOnline(sdf.parse(json_data.getString("LastOnline")));
		
		return userbuffer;
	}
	
	public static Participant ParticipantFromJsonString(String jsondata) throws NumberFormatException, JSONException, ParseException {
		JSONObject json_data = new JSONObject(jsondata);
		Participant participantbuffer = new Participant();
		participantbuffer.setId(Integer.parseInt(json_data.getString("UId")));
		participantbuffer.setUsername(json_data.getString("Username"));

		int adminbuffer = 0;
		adminbuffer = json_data.getInt("isAdmin");
		if(adminbuffer == 1)
			participantbuffer.setAdmin(true);
		else
			participantbuffer.setAdmin(false);
		
		participantbuffer.seteMail(json_data.getString("EMail"));
		participantbuffer.setFirstName(json_data.getString("Firstname"));
		participantbuffer.setLastName(json_data.getString("Lastname"));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		participantbuffer.setBirthDay(sdf.parse(json_data.getString("Birthday")));
		participantbuffer.setLastOnline(sdf.parse(json_data.getString("LastOnline")));
		participantbuffer.setEventid(Integer.parseInt(json_data.getString("Eventid")));
		participantbuffer.setFollowers(Integer.parseInt(json_data.getString("Followers")));
		participantbuffer.setAcceptdate(sdf.parse(json_data.getString("Accepttime")));
		return participantbuffer;
	}
	
	public static Event EventFromJsonString(String jsondata) throws NumberFormatException, JSONException, ParseException {
		
		JSONObject json_data = new JSONObject(jsondata);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
		Event eventbuffer = new Event();
		  
        eventbuffer.setId(Integer.parseInt(json_data.getString("EId")));
        eventbuffer.setName(json_data.getString("Name"));
        eventbuffer.setPlace(json_data.getString("Place"));
        eventbuffer.setDescription(json_data.getString("Description"));
		eventbuffer.setStartDate(sdf.parse((json_data.getString("Starttime"))));
		eventbuffer.setEndDate(sdf.parse((json_data.getString("Endtime"))));
		eventbuffer.setCreationDate(sdf.parse(json_data.getString("Editdate")));
		
		User creator = new User();
		creator.setId(Integer.parseInt(json_data.getString("UId")));
		creator.setUsername(json_data.getString("Username"));
		Log.d("warning", eventbuffer.getId() + " username null");
        
		int adminbuffer = 0;
		Boolean admin = false;
		adminbuffer = json_data.getInt("isAdmin");
		if(adminbuffer == 1)
			admin = true;
		
		creator.setAdmin(admin);
        
		eventbuffer.setCreator(creator);

		return eventbuffer;
	}
	
	public static String UserAsJSONString(User user) throws JSONException {
		JSONObject jsonuser = new JSONObject();
		jsonuser.put("UId", user.getId());
		jsonuser.put("Username", user.getUsername());
		jsonuser.put("isAdmin", user.isAdmin());
		jsonuser.put("EMail", user.geteMail());
		jsonuser.put("Firstname", user.getFirstName());
		jsonuser.put("Lastname", user.getLastName());
		jsonuser.put("Birthday", user.getBirthDay());
		jsonuser.put("LastOnline", user.getLastOnline());
		
		return jsonuser.toString();

	}
	
	public static String ParticipantAsJSONString(Participant participant) throws JSONException {
		JSONObject jsonuser = new JSONObject();
		jsonuser.put("UId", participant.getId());
		jsonuser.put("Username", participant.getUsername());
		jsonuser.put("isAdmin", participant.isAdmin());
		jsonuser.put("EMail", participant.geteMail());
		jsonuser.put("Firstname", participant.getFirstName());
		jsonuser.put("Lastname", participant.getLastName());
		jsonuser.put("Birthday", participant.getBirthDay());
		jsonuser.put("LastOnline", participant.getLastOnline());
		jsonuser.put("Eventid", participant.getEventid());
		jsonuser.put("Followers", participant.getFollowers());
		
		return jsonuser.toString();

	}
}